package cliente;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author triki
 */

import aso.upna.exceptions.MediaPlayerNotReady;
import aso.upna.exceptions.NoFileLoaded;
import aso.upna.filesystem.SharedFolder;
import aso.upna.threads.ClientListenerThread;
import aso.upna.threads.ServerListenerThread;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;


public class AudioPlayer {
    private static final int BYTES = 100;
    private static final int BUFFER_SIZE =10000;
   
    public static void main(String[] args) throws MediaPlayerNotReady {

        byte[] aux = new byte[10];
        InputStream is = null;

        SharedFolder sf = new SharedFolder("./");
        sf.listFiles();
        
        ServerListenerThread server = new ServerListenerThread(9999);
        Thread hiloServer = new Thread(server);
        hiloServer.start();
        ClientListenerThread client = new ClientListenerThread();
        Thread hiloClient = new Thread(client);
        client.run();
        /*try
        {
            Socket sock = new Socket("127.0.0.1", 9999);
            is = sock.getInputStream();
            File f = new File("./I_Can(prueba).mp3");   
            FileOutputStream os = new FileOutputStream(f);
            int i=0;
            
            Hilo runnable = new Hilo(sock,"./I_Can(prueba).mp3");
            Thread thread = new Thread(runnable);

            //Una vez instanciado la clase es necesario inicializar el reproductor
            //Esto podra a punto todos los elementos del sistema.
            runnable.startUpAudioPlayer();

            System.out.println("Buffering...");

            int delay = 1000; //milliseconds
            ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                      //Accion a ejecutar cuando salte la alarma.
                  }
              };
              
            new Timer(delay, taskPerformer).start();

            while (is.read(aux) != -1)
            {
                os.write(aux);
                i++;
                
                if(i == 10000)
                {
                    thread.start();
                }

                //runnable.getCurrentDuration();
                //runnable.getCurrentPosition();

            }
            System.out.println("Transferencia de fichero Terminada.");
            System.in.read();
            runnable.getCurrentDuration();
            runnable.getCurrentPosition();
            System.in.read();
            runnable.stop();

        } catch (IOException ex) {
           ex.printStackTrace();
        }*/
    }
}
